package com.passive.lock.controller.lock;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.passive.lock.controller.BaseController;
import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TLockVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.lock.LockService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("lo")
public class LockController extends BaseController {
	@Autowired
	private LockService lockService;

	/**
	 * 锁具信息管理
	 *
	 */

	@RequestMapping(value = "/locklist")
	@ResponseBody
	public Result lockList(@RequestParam(required = false) String deptid,
			@RequestParam(required = false) String lockName, @RequestParam(required = false) String lockCode,
			@RequestParam(required = false) String lockType, @RequestParam Integer page, @RequestParam Integer limit,
			HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		String groupid = tUserVo.getGroupid();
		try {
			int offset = PageUtil.getOffset(page, limit);
			List<TLockVo> list = lockService.lockList(groupid, deptid, lockName, lockCode, lockType, offset, limit);
			int count = lockService.lockListCount(groupid, deptid, lockName, lockCode, lockType);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/addlock")
	@ResponseBody
	public Result addLock(@RequestBody TLockVo tLockVo) {
		try {
			Result result = lockService.addLock(tLockVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updatelock")
	@ResponseBody
	public Result updateLock(@RequestBody TLockVo tLockVo) {
		try {
			Result result = lockService.updateLock(tLockVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletelock")
	@ResponseBody
	public Result deleteLock(@RequestBody TLockVo tLockVo) {
		try {
			Result result = lockService.deleteLock(tLockVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deletelocklist")
	@ResponseBody
	public Result deleteLockList(@RequestBody List<Integer> lockids) {
		try {
			Result result = lockService.deleteLockList(lockids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/querylocklist")
	@ResponseBody
	public Result queryLockList(@RequestParam Integer regionId, @RequestParam(required = false) Integer routeId,HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		String groupid = tUserVo.getGroupid();
		try {
			Result result = lockService.queryLockList(regionId,routeId,groupid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	
	

}
